package com.xr.system.common.config.satokenConfig;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import com.xr.system.common.other.StringToEnumConverterFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {

    // 注册Sa-Token的注解拦截器，打开注解式鉴权功能
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//		// 注册注解拦截器，并排除不需要注解鉴权的接口地址 (与登录拦截器无关)
//		registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**");
//		// 注册Sa-Token的路由拦截器
//		registry.addInterceptor(new SaRouteInterceptor()).addPathPatterns("/**").excludePathPatterns(
//				"/api/v1/login/login", "/api/v1/account/verificationCode", "/doc.html", "/webjars/**",
//				"/swagger-resources/**", "/v2/api-docs", "/index.html", "/css/**", "/fonts/**", "/js/**",
//				"/favicon.ico");
        // 注册SaToken注解鉴权拦截器（新版）
        registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**");
        // 注册SaToken路由拦截器（新版）
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin())).addPathPatterns("/**")
                .excludePathPatterns("/api/v1/login/login", "/api/v1/account/verificationCode", "/doc.html",
                        "/webjars/**", "/swagger-resources/**", "/v2/api-docs", "/v3/api-docs", "/index.html",
                        "/css/**", "/fonts/**", "/js/**", "/favicon.ico", "/error");
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(new StringToEnumConverterFactory());
    }

}
